package jpa.lia.edu.ar;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;

public class Main {
    public static void main(String[] args) {
       
        EntityManagerFactory emf =
            Persistence.createEntityManagerFactory("$objectdb/db/persona.odb");
        EntityManager em = emf.createEntityManager();
 
       
        em.getTransaction().begin();
        long tiempoInicio = System.currentTimeMillis();
		        for (int i = 0; i < 1000; i++) {
		            //Point p = new Point(i, i);
		            
		            Person ps = new Person(new Integer(i).toString(), i, Sexo.MASCULINO);
		            Adress a = new Adress("test", 223);
		            
		            Email e = new Email("dsdfs@sdasd.com",ps);
		            em.persist(e);
		            ps.getEmails().add(new Email(i,"dsdfs@sdasd.com",ps));
		            em.persist(a);
		            ps.setAdress(a);
		            em.persist(ps);
		          //  em.persist(p);
		        }
        em.getTransaction().commit();
        Query q1 = em.createQuery("SELECT COUNT(p) FROM Person  p");
        System.out.println("Total Points: " + q1.getSingleResult());
        
        Query q2 = em.createQuery("SELECT AVG(p.edad) FROM Person p");
        System.out.println("Average X: " + q2.getSingleResult());
        
        
        Query q3 = em.createQuery("SELECT e.person.adress.street  FROM Email e");
        System.out.println("Stret X: " + q3.getResultList().get(0));
        
        long totalTiempo = System.currentTimeMillis() - tiempoInicio;
        System.out.println("El tiempo de demora es :" + totalTiempo + " miliseg");
        
        
					 /*
					        // Find the number of Point objects in the database:
					        Query q1 = em.createQuery("SELECT COUNT(p) FROM Point p");
					        System.out.println("Total Points: " + q1.getSingleResult());
					 
					        // Find the average X value:
					        Query q2 = em.createQuery("SELECT AVG(p.x) FROM Point p");
					        System.out.println("Average X: " + q2.getSingleResult());
					 
					        // Retrieve all the Point objects from the database:
					        TypedQuery<Point> query =
					            em.createQuery("SELECT p FROM Point p", Point.class);
					        List<Point> results = query.getResultList();
					        for (Point p : results) {
					            System.out.println(p);
					        }
					 */
        
        em.close();
        emf.close();
    }
}